package com.zjitc.controller;

import com.zjitc.common.ServerResponse;
import com.zjitc.model.JsonData;
import com.zjitc.model.User;
import com.zjitc.service.UserService;
import java.util.Date;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Aspire on 2018/3/28.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login.do")
    @ResponseBody
    public ServerResponse login(
        HttpSession session, String username, String password){
        ServerResponse serverResponse = userService.login(username, password);
        if (serverResponse.getStatus() == 1) {
            return serverResponse;
        }
        session.setAttribute("user", serverResponse.getData());
        return serverResponse;
    }

    @RequestMapping("/register.do")
    @ResponseBody
    public ServerResponse register(User user){
        user.setRole(0);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        return userService.register(user);
    }
    @RequestMapping("/check_valid.do")
    @ResponseBody
    public ServerResponse check_valid(HttpSession session, @RequestParam("str") String str, @RequestParam("type") String type){
        User uuser=(User)session.getAttribute("user");
        if (uuser==null){
            return ServerResponse.createError("用户未登录");
        }
        ServerResponse<User> serverResponse=userService.check_valid(str,type);
        return serverResponse;
    }

    @RequestMapping("/get_user_info.do")
    @ResponseBody
    public ServerResponse get_user_info(HttpSession session){
        User uuser=(User)session.getAttribute("user");
        if (uuser==null){
            return ServerResponse.createError("用户未登录");
        }
        ServerResponse<User> serverResponse=userService.get_user_info(session);
        return serverResponse;
    }

    @RequestMapping("/forget_get_question.do")
    @ResponseBody
    public ServerResponse forget_get_question(String username){
       ServerResponse<User> serverResponse=userService.forget_get_question(username);
       return serverResponse;
    }
    @RequestMapping("forget_check_answer.do")
    @ResponseBody
    public ServerResponse forget_check_answer(String username,String question,String answer){
        ServerResponse<User> serverResponse=userService.forget_check_answer(username,question,answer);
        return serverResponse;
    }

    @RequestMapping( "forget_reset_password.do")
    @ResponseBody
    public ServerResponse<String> forget_reset_password(String username,String passwordNew,String forgetToken){
        return userService.forget_reset_password(username, passwordNew, forgetToken);
    }

    @RequestMapping( "reset_password.do")
    @ResponseBody
    public ServerResponse<String> reset_password(HttpSession session,String passwordOld,String passwordNew){
        User user = (User)session.getAttribute("user");
        if(user == null){
            return ServerResponse.createError("用户未登录");
        }
        return userService.reset_password(passwordOld,passwordNew,user);
    }


    @RequestMapping("update_information.do")
    @ResponseBody
    public ServerResponse<User> update_information(HttpSession session,User user){
      User uuser=(User)session.getAttribute("user");
      if (uuser==null){
          return ServerResponse.createError("用户未登录");
      }
      user.setId(uuser.getId());
      user.setRole(uuser.getRole());
      user.setUsername(uuser.getUsername());
      user.setCreateTime(uuser.getCreateTime());
      ServerResponse<User> serverResponse=userService.update_information(user);

       return serverResponse;
    }


    @RequestMapping("get_information.do")
    @ResponseBody
    public ServerResponse<User> get_information(HttpSession session){
        User user= (User) session.getAttribute("user");
        if (user==null){
            return ServerResponse.createError("用户未登录,无法获取当前用户信息,status=10,强制登录");
        }
        return ServerResponse.createSuccess(user) ;
    }




    @RequestMapping("logout.do")
    @ResponseBody
    public ServerResponse<User> logout(HttpSession session){
        session.removeAttribute("user");
        return ServerResponse.createSuccess("退出成功");
    }

}
